package com.sky.mapper;

import com.sky.dto.UserReportDTO;
import com.sky.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * @description: 根据openid微信用户唯一标识获取用户信息
     * @author: tanbobin
     * @date: 2024/6/4 20:54
     * @param:
     * @param openid
     * @return:
     * @return com.sky.entity.User
     **/
    @Select("select * from user where openid = #{openid}")
    User selectByOpenid(String openid);

    /**
     * @description: 插入用户信息
     * @author: tanbobin
     * @date: 2024/6/4 20:54
     * @param:
     * @param user
     * @return:
     **/
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user (openid, name, phone, sex, id_number, avatar, create_time) values" +
            "(#{openid},#{phone},#{phone},#{sex},#{idNumber},#{avatar},#{createTime})")
    void insert(User user);

    /**
     * @description:
     * @author: tanbobin
     * @date: 2024/6/6 18:01
     * @param:
     * @param userId
     * @return:
     * @return com.sky.entity.User
     **/
    @Select("select * from USER where id = #{id}")
    User getById(Long userId);

    /**
     * @description: 统计每一天的新增用户数
     * @author: tanbobin
     * @date: 2024/6/12 15:59
     * @param:
     * @param beginTime
     * @param endTime
     * @return:
     * @return java.util.List<com.sky.dto.UserReportDTO>
     **/
    List<UserReportDTO> countAddByCreateTime(LocalDateTime beginTime, LocalDateTime endTime);

    /**
     * 统计 截止到指定时间的 用户的总数量
     * @param beginTime
     * @return:
     **/
    @Select("select count(*) from user where create_time < #{beginTime}")
    Integer countTotalByCreateTime(LocalDateTime beginTime);

    /**
     * @description: 根据时间范围统计新增用户数
     * @author: tanbobin
     * @date: 2024/6/13 11:13
     * @param:
     * @param beginTime
     * @param endTime
     * @return:
     * @return java.lang.Integer
     **/
    Integer countByTime(LocalDateTime beginTime, LocalDateTime endTime);
}
